using Microsoft.AspNetCore.Mvc;
using SupplierCustomerManagement.Read.API.Dtos;
using SupplierCustomerManagement.Read.API.Dtos.Breeding;
using MediatR;
using SupplierCustomerManagement.Read.API.Applications.Commands.breeding;

namespace SupplierCustomerManagement.Read.API.Controllers
{
    /// <summary>
    /// 配种管理
    /// </summary>
    [ApiController]
    [Route("api/breeding")]
    public class BreedingController : ControllerBase
    {
        private readonly ILogger<BreedingController> _logger;
        private readonly IMediator _mediator;

        public BreedingController(ILogger<BreedingController> logger, IMediator mediator)
        {
            _logger = logger;
            _mediator = mediator;
        }

        /// <summary>
        /// 获取羊场实时存栏预估率数据
        /// </summary>
        [HttpGet("estimated-rate")]
        public async Task<ActionResult<ApiResponse<List<EstimatedRateRowDto>>>> GetEstimatedRate([FromQuery] string date)
        {
            try
            {
                var result = await _mediator.Send(new GetEstimatedRateQuery { Date = date });
                var resp = ApiResponse<List<EstimatedRateRowDto>>.Ok(result);
                return Ok(resp);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取实时存栏预估率失败");
                return StatusCode(500, ApiResponse<List<EstimatedRateRowDto>>.Fail($"获取实时存栏预估率失败: {ex.Message}"));
            }
        }

        /// <summary>
        /// 获取配种分娩率统计
        /// </summary>
        [HttpGet("breeding-rate")]
        public async Task<ActionResult<ApiResponse<BreedingRateDto>>> GetBreedingRate([FromQuery] string year)
        {
            try
            {
                var result = await _mediator.Send(new GetBreedingRateQuery { Year = year });
                var resp = ApiResponse<BreedingRateDto>.Ok(result);
                return Ok(resp);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取配种分娩率统计失败");
                return StatusCode(500, ApiResponse<BreedingRateDto>.Fail($"获取配种分娩率统计失败: {ex.Message}"));
            }
        }

        /// <summary>
        /// 获取羊群状态分布
        /// </summary>
        [HttpGet("status-distribution")]
        public async Task<ActionResult<ApiResponse<StatusDistributionDto>>> GetStatusDistribution([FromQuery] string? date, [FromQuery] string? farmId)
        {
            try
            {
                int? farm = null;
                if (!string.IsNullOrWhiteSpace(farmId) && int.TryParse(farmId, out var fid))
                {
                    farm = fid;
                }
                var result = await _mediator.Send(new GetStatusDistributionQuery { Date = date, FarmId = farm });
                var resp = ApiResponse<StatusDistributionDto>.Ok(result);
                return Ok(resp);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取羊群状态分布失败");
                return StatusCode(500, ApiResponse<StatusDistributionDto>.Fail($"获取羊群状态分布失败: {ex.Message}"));
            }
        }
    }
}
